package com.gqc.test08.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.gqc.pojo.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public int add() {
		String sql = "insert into user(name,age) values(?,?)";
		int i = jdbcTemplate.update(sql, "rose",18);
		return i;
	}

	@Override
	public int delete() {
		String sql = "delete from user where id=?";
		int i = jdbcTemplate.update(sql, 1);
		return i;
	}

	@Override
	public int update() {
		String sql = "update user set name = ? where id=?";
		int i = jdbcTemplate.update(sql, "TOM",2);
		return i;
	}

	@Override
	public User findById() {
		String sql = "select * from user where id=?";
		RowMapper<User> rowMapper 
			= new BeanPropertyRowMapper<User>(User.class);
		User User = jdbcTemplate.queryForObject(sql, rowMapper,3);
		return User;
	}

	@Override
	public List<User> findUser() {
		String sql = "select * from user";
		RowMapper<User> rowMapper 
			= new BeanPropertyRowMapper<User>(User.class);
		List<User> list = jdbcTemplate.query(sql,rowMapper);
		return list;
	}

}
